<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新闻动态-demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}">
    <script th:src="@{/js/jquery-3.4.1.min.js}"></script>
    <script th:src="@{/js/bootstrap.bundle.min.js}"></script>
    <script th:src="@{/js/jquery.twbsPagination.js}"></script>

    <style type="text/css">
        body{
            background-color: #f5f5f5;
            padding-top: 80px;
        }
        html {
            overflow-y: scroll;
        }
        :root {
            overflow-y: auto;
            overflow-x: hidden;
        }
        :root body {
            position: absolute;
        }
        body {
            width: 100vw;
            overflow: hidden;
        }
    </style>
</head>
<body>

<div th:include="layout/header :: header(user=${session.user},flag=${session.user}!=null,page='news_list')"></div>

<main role="main" class="container">
    <div class="bg-white rounded shadow">
        <div class="card-header py-3 text-center bg-danger text-light">
            <h5 class="card-title mb-0">新闻</h5>
        </div>
        <ul class="list-group pt-2 px-3" id="content">
        </ul>
        <div class="d-flex justify-content-center mt-2" id="pagination-demo"></div>

    </div>

</main>

<div th:include="layout/footer :: Footer"></div>
<script type="text/javascript">
    // $(document).ready(function () {
    //     getPage(1);
    // });

    $('#pagination-demo').twbsPagination({
        totalPages: [[${total}]],
        visiblePages: 5,
        onPageClick: function (event, page) {
            getPage(page);
        }
    });

    function getPage(page){
        $.ajax({//根据page去后台加载数据
            url : "/news/getNewsList",
            type : "get",
            dataType : "json",
            data : {"page" : page},
            success : function(data) {
                appendHtml(data.content);//处理数据
                console.log(data);
            }
        });
    }
    function appendHtml(list) {//此函数用于处理后台返回的数据，根据自己需求来实现页面拼接
        let tableShow = '';
        for (let i = 0; i < list.length; i++) {
            tableShow += '<li class="list-group-item border-0 px-3 py-2">\n' +
                '                <div class="row mx-0 pb-2 border-bottom border-gray">\n' +
                '                    <a class="ml-1" href="/news?newsID='+list[i].newsID+'" target="_blank">'+list[i].title+'</a>\n' +
                '                    <div class="ml-auto">\n' +
                '                        <span class="badge badge-secondary">'+list[i].time+'</span>\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </li>\n';
        }
        $('#content').html(tableShow);
    }
</script>
</body>
</html>